package com.server.impl;

import com.dao.product.ProductMapper;
import com.entry.PageBean;
import com.entry.Product;
import com.server.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;

    @Override
    public int selectCount() {
        return productMapper.selectCount();
    }

    @Override
    public List<Product> getproductList() {
        return productMapper.getproductList();
    }

    @Override
    public List<Product> getproducandproductimageList() {
        return productMapper.getproducandproductimgtList();
    }

    @Override
    public PageBean<Product> findProductByPage(int currentPage) {
        HashMap<String,Object> map=new HashMap<String,Object>();
        PageBean<Product> pageBean=new PageBean<Product>();

        //封装当前页数
        pageBean.setCurrPage(currentPage);

        //每页显示的数据
        int pageSize=16;
        pageBean.setPageSize(pageSize);

        //封装总记录数
        int totalCount = productMapper.selectCount();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());

        map.put("start",(currentPage-1)*pageSize);
        map.put("size", pageBean.getPageSize());
        //封装每页显示的数据
        List<Product> lists = productMapper.findeByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }

    @Override
    public List<Product> SelectNameCount(String SelectName) {
        return productMapper.SelectNameCount(SelectName);
    }

    @Override
    public Product getproductByProduct_id(int product_id) {
        return productMapper.getproductByProduct_id(product_id);
    }
}
